package com.qxzn.app.personalcenter.activity;

import android.os.Bundle;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;

import com.qxzn.app.page.BaseActivity;
import com.qxzn.app.personalcenter.R;
import com.qxzn.app.personalcenter.R2;
import com.qxzn.app.util.AppUtil;
import com.qxzn.app.util.CommonUtil;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 注册4-昵称
 */
public class RegisterNicknameActivity extends BaseActivity {
    @BindView(R2.id.et_nickname)
    EditText etNickname;
    @BindView(R2.id.rgp_sex)
    RadioGroup rgpSex;
    private String sex = "1";
    private String nickname;

    @Override
    protected int initLayout() {
        return R.layout.activity_register_nickname;
    }

    @Override
    protected void initView() {
        rgpSex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rbtn_male) {
                    sex = "1";
                    AppUtil.showLog("男");
                } else if (checkedId == R.id.rbtn_female) {
                    sex = "2";
                    AppUtil.showLog("女");
                }
            }
        });
    }

    @Override
    protected void iniTitleBar() {
        tv_title.setVisibility(View.GONE);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        etNickname.setFilters(new InputFilter[]{new InputFilter.LengthFilter(6), new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                Pattern p = Pattern.compile("[\u4e00-\u9fa5]+");
                Matcher m = p.matcher(source.toString());
                if (!m.matches()) return "";
                return null;
            }
        }});
    }

    @OnClick(R2.id.tv_next)
    public void onViewClicked() {
        if (CommonUtil.isFastClick()) {
            return;
        }
        nickname = etNickname.getText().toString().trim();
        if (TextUtils.isEmpty(nickname)) {
            AppUtil.showToast(this, etNickname.getHint().toString());
            return;
        }
        if (nickname.length() > 6 || !CommonUtil.isChineseChar(nickname)) {
            AppUtil.showToast(this, "昵称最多只能是6个中文字符哦");
            return;
        }
        startActivity(getIntent().setClass(this, RegisterFace1Activity.class)
                .putExtra("nickname", etNickname.getText().toString().trim())
                .putExtra("sex", sex));
    }
}
